package vip.specimen.system.service;

import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.specimen.system.datasource.entity.SystemArticleEntity;
import vip.specimen.system.datasource.entity.SystemUserEntity;
import vip.specimen.system.repository.SystemArticleRepository;
import vip.specimen.system.datasource.validation.SystemArticleAttributeValidation;
import vip.specimen.system.datasource.validation.SystemArticleValidation;
import vip.specimen.system.utils.CommonUtil;
import vip.specimen.system.utils.DataBaseUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * *@Author: 余浪人
 * *@Date：2021/4/9 10:12
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Service
@Transactional
public class SystemArticleService {
    @Resource
    private SystemArticleRepository articleRepository;
    @Resource
    private SystemArticleCategoriesService articleCategoriesService;
    @Resource
    private SystemArticleTagsService articleTagsService;
    @Resource
    private HttpServletRequest request;

    public SystemArticleEntity insert(SystemArticleValidation articleValidation) {
        SystemUserEntity user = (SystemUserEntity) SecurityUtils.getSubject().getPrincipal();
        return this.articleRepository.save(new SystemArticleEntity(
                articleValidation.getTitle(), articleValidation.getSource(), articleValidation.getCategories().size() != 0 ? this.articleCategoriesService.findAllById(articleValidation.getCategories()) : null,
                null != articleValidation.getAuthor() ? articleValidation.getAuthor() : (null != user ? user.getUsername() : ""),
                articleValidation.getThumb(), articleValidation.getState(), articleValidation.getContent(),
                null != articleValidation.getTags() && articleValidation.getTags().size() != 0 ? this.articleTagsService.findAllById(articleValidation.getTags()) : null,
                articleValidation.getKeyword(), articleValidation.getDescription(), articleValidation.getClick(),
                articleValidation.getFocus(), articleValidation.getRoll(), articleValidation.getTop(), articleValidation.getRecommend(),
                articleValidation.getContribute(), articleValidation.getOther()
        ));
    }

    public Page<SystemArticleEntity> findAllPage() {
        JSONObject param = CommonUtil.pageParams(this.request);
        return this.articleRepository.findAll(DataBaseUtil.getPageable(param.getIntValue("page"), param.getIntValue("pageSize"), DataBaseUtil.getSort("desc", "createTime")));
    }

    /**
     * 根据状态
     *
     * @param uuid uuid
     * @return entity
     */
    public SystemArticleEntity update(UUID uuid) {
        if (this.articleRepository.existsById(uuid)) {
            Optional<SystemArticleEntity> o = this.articleRepository.findById(uuid);
            if (!o.isPresent()) return null;
            SystemArticleEntity articleEntity = o.get();
            articleEntity.setEnable(!articleEntity.isEnable());
            articleEntity.setUpdateTime(Calendar.getInstance().getTime());
            this.articleRepository.save(articleEntity);
            return articleEntity;
        }
        return null;
    }

    /**
     * 更新内容
     *
     * @param uuid              uuid
     * @param articleValidation 验证体
     * @return entity
     */
    public SystemArticleEntity update(UUID uuid, SystemArticleValidation articleValidation) {
        if (null != uuid && this.articleRepository.existsById(uuid)) {
            Optional<SystemArticleEntity> o = this.articleRepository.findById(uuid);
            if (!o.isPresent()) return null;
            SystemArticleEntity articleEntity = o.get();
            articleEntity.setTitle(articleValidation.getTitle());
            articleEntity.setOther(articleValidation.getOther());
            articleEntity.setFocus(articleValidation.getFocus());
            articleEntity.setRoll(articleValidation.getRoll());
            articleEntity.setTop(articleValidation.getTop());
            articleEntity.setRecommend(articleValidation.getRecommend());
            articleEntity.setContribute(articleValidation.getContribute());
            articleEntity.setOther(articleValidation.getOther());
            articleEntity.setClick(articleValidation.getClick());
            articleEntity.setAuthor(articleValidation.getAuthor());
            articleEntity.setContent(articleValidation.getContent());
            articleEntity.setDescription(articleValidation.getDescription());
            articleEntity.setKeyword(articleValidation.getKeyword());
            articleEntity.setSource(articleValidation.getSource());
            articleEntity.setCategories(this.articleCategoriesService.findAllById(articleValidation.getCategories()));
            articleEntity.setThumb(articleValidation.getThumb());
            articleEntity.setState(articleValidation.getState());
            articleEntity.setTags(null != articleValidation.getTags() && articleValidation.getTags().size() != 0 ? this.articleTagsService.findAllById(articleValidation.getTags()) : null);
            articleEntity.setUpdateTime(Calendar.getInstance().getTime());
            return this.articleRepository.save(articleEntity);
        }
        return null;
    }

    /**
     * 更新属性
     *
     * @param attributeValidation 属性验证体
     * @return entity
     */
    public SystemArticleEntity update(SystemArticleAttributeValidation attributeValidation) {
        if (null != attributeValidation.getUuid() && this.articleRepository.existsById(attributeValidation.getUuid())) {
            Optional<SystemArticleEntity> o = this.articleRepository.findById(attributeValidation.getUuid());
            if (!o.isPresent()) return null;
            SystemArticleEntity articleEntity = o.get();
            if (null != attributeValidation.getFocus()) articleEntity.setFocus(attributeValidation.getFocus());
            if (null != attributeValidation.getRoll()) articleEntity.setRoll(attributeValidation.getRoll());
            if (null != attributeValidation.getTop()) articleEntity.setTop(attributeValidation.getTop());
            if (null != attributeValidation.getRecommend()) articleEntity.setRecommend(attributeValidation.getRecommend());
            if (null != attributeValidation.getContribute()) articleEntity.setContribute(attributeValidation.getContribute());
            if (null != attributeValidation.getOther()) articleEntity.setOther(attributeValidation.getOther());
            articleEntity.setUpdateTime(Calendar.getInstance().getTime());
            this.articleRepository.save(articleEntity);
            return articleEntity;
        }
        return null;
    }

    /**
     * 删除内容
     *
     * @param uuid uuid
     * @return res
     */
    public boolean delete(UUID uuid) {
        if (this.articleRepository.existsById(uuid)) {
            this.articleRepository.deleteById(uuid);
            return true;
        }
        return false;
    }

    /**
     * 批量删除
     *
     * @param uuidList uuidList
     * @return successNum
     */
    public int delete(Set<UUID> uuidList) {
        int successNum = 0;
        for (UUID uuid : uuidList) {
            successNum += this.delete(uuid) ? 1 : 0;
        }
        return successNum;
    }

    /**
     * 根据标题查询
     *
     * @param title 标题
     * @return entity
     */
    public SystemArticleEntity findByTitle(String title) {
        return this.articleRepository.findByTitle(title);
    }

    /**
     * 统计条数
     *
     * @return res
     */
    public long count() {
        return this.articleRepository.count();
    }
}
